package com.dp.pub;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendMail {

	public static void send(String title,String cmail)
	{
		 try{
	         
	                    String mailserver =SysProperty.getPrep("mail.mailserver");
	                    String from = SysProperty.getPrep("mail.from");
	                     String username=SysProperty.getPrep("mail.username");
	                     String passwd=SysProperty.getPrep("mail.passwd");
	                    String to = SysProperty.getPrep("mail.to"); 
	                    if(to == null || "".equals(to))
	                    	return;
	                    String []tos=to.split(",");
	                     Properties prop =new Properties();
	                     prop.put("mail.transport.default","smtp");
	                     prop.put("mail.smtp.auth", "true");
	                     prop.put("mail.smtp.host",mailserver);
	                    
	                    
	                	 Session mailSession = Session.getInstance(prop,new MyAuthenticator(username,passwd));
	                	 
	                     Message msg = new MimeMessage(mailSession);	                     
						         msg.setFrom(new InternetAddress(from));
	                    	 //  log.info(subject+message);
	                    	   msg.setContent(new String(cmail.getBytes("gb2312"),"ISO8859-1"), "text/plain"); 
	                    	   msg.setSubject(title);
	                    	  // msg.setSubject(new   String(subject.getBytes("GB2312")));---
	                    	   Transport ts=null;
	                    	   InternetAddress [] ad=new InternetAddress[tos.length];
	                    	   for(int i=tos.length-1;i>-1;i--)ad[i]=new InternetAddress(tos[i]);
				                   //msg.setRecipient(Message.RecipientType.TO, new InternetAddress(s));
	                    	       msg.setRecipients(Message.RecipientType.TO, ad);
				                   msg.saveChanges();	                
				                   ts=mailSession.getTransport("smtp");
				                  // ts.connect(username,passwd);
				                  // ts.send(msg,msg.getAllRecipients());
				                   ts.send(msg);
				                   ts.close();
				               
			            
	  }catch(Exception e){e.getMessage();}
	}
		  
}
class MyAuthenticator  extends javax.mail.Authenticator {
    private String strUser;
    private String strPwd;
    public MyAuthenticator(String user, String password) {
      this.strUser = user;
      this.strPwd = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(strUser, strPwd);
    }
  }
